<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:p="http://primefaces.org/ui"
                template="../../template/tagedu.xhtml">


    <ui:define name="title">
        <title><h:outputText value="TAG - Horário da Unidade"/></title>
        <script type="text/javascript">
            PrimeFaces.locales['en'] = {
                closeText: 'Fechar',
                prevText: 'Anterior',
                nextText: 'Próximo',
                currentText: 'Começo',
                monthNames: ['Janeiro','Fevereiro','Março','Abril','Maio','Junho','Julho','Agosto','Setembro','Outubro','Novembro','Dezembro'],
                monthNamesShort: ['Jan','Fev','Mar','Abr','Mai','Jun', 'Jul','Ago','Set','Out','Nov','Dez'],
                dayNames: ['Domingo','Segunda','Terça','Quarta','Quinta','Sexta','Sábado'],
                dayNamesShort: ['Dom','Seg','Ter','Qua','Qui','Sex','Sáb'],
                dayNamesMin: ['D','S','T','Q','Q','S','S'],
                weekHeader: 'Semana',
                firstDay: 0,
                isRTL: false,
                showMonthAfterYear: false,
                yearSuffix: '',
                timeOnlyTitle: 'Só Horas',
                timeText: 'Tempo',
                hourText: 'Hora',
                minuteText: 'Minuto',
                secondText: 'Segundo',
                ampm: false,
                currentText: 'Data Atual',
                month: 'Mês',
                week: 'Semana',
                day: 'Dia',
                allDayText : 'Todo Dia'
            };
        </script>
        <style type="text/css">
            tr.fc-first th{text-indent:-9999px;}
        </style>
    </ui:define>

    <ui:define name="content">
        <h:outputText styleClass="organizationName" value="Horário Semanal de #{unityScheduleMB.organization.name}"/>
        <h:form id="scheduleForm">
            <p:message for="scheduleForm" id="messages"/>

            <h:panelGroup id="scheduleParam" layout="block">
                <h:panelGroup layout="block" class="input-list">
                    <h:outputLabel for="year" value="Ano: " class="label"/>
                    <p:spinner id="year" value="#{unityScheduleMB.year}" min="#{unityScheduleMB.actualYear - 1}" max="#{unityScheduleMB.actualYear + 1}" size="4">
                        <f:validator validatorId="yearV"/>
                        <f:attribute name="minimo" value="#{(unityScheduleMB.actualYear - 1).toString()}"/>
                        <f:attribute name="maximo" value="#{(unityScheduleMB.actualYear + 1).toString()}"/>
                        <p:ajax process="@this" update="unities"
                                listener="#{unityScheduleMB.loadUnityList()}"/>
                    </p:spinner>
                </h:panelGroup>
                <h:panelGroup id="unities" layout="block" class="input-list">
                    <h:outputLabel for="unity" value="Unidade: " class="label"/>
                    <p:selectOneMenu id="unity" value="#{unityScheduleMB.unity}" converter="unityC">
                        <f:selectItem value="#{null}" itemLabel="Selecione uma unidade"/>
                        <f:selectItems value="#{unityScheduleMB.unityList}" var="u" itemLabel="#{u.name}" itemValue="#{u}"/>
                        <p:ajax process="@this" update="scheduleGroup"
                                listener="#{unityScheduleMB.loadSchedule()}"/>
                    </p:selectOneMenu>
                </h:panelGroup>
            </h:panelGroup>
            <div class="clear"/>

            <h:panelGroup id="scheduleGroup">
                <br/>
                <p:schedule resizable="false" locale="pt" id="schedule" draggable="false"
                            rightHeaderTemplate="month" view="agendaWeek" value="#{unityScheduleMB.unityScheduleModel}"
                            firstHour="#{unityScheduleMB.firstHour}" showHeader="false" allDaySlot="false"
                            widgetVar="uschedule" slotMinutes="15" 
                            rendered="#{unityScheduleMB.scheduleVisible}">
                    <p:ajax event="dateSelect" listener="#{unityScheduleMB.onDateSelect}"
                            update=":manageUEDetails" oncomplete="manageUEDialogW.show();" />
                    <p:ajax event="eventSelect" listener="#{unityScheduleMB.onEventSelect}"
                            update=":manageUEDetails" oncomplete="manageUEDialogW.show();" />
                </p:schedule>
            </h:panelGroup>
        </h:form>
    </ui:define>

    <ui:define name="modals">
        <p:dialog id="manageUEDialog" position="top" widgetVar="manageUEDialogW"
                  header="Detalhes do horário" showEffect="clip" hideEffect="clip" closable="true" 
                  width="800" modal="true" styleClass="scheduleEdit">
            <h:form id="manageUEDetails">
                <p:messages/>
                <h:panelGrid columns="2" cellpadding="15">
                    <h:outputLabel for="createEditTaskSelect" value="Tarefa:" />
                    <h:selectOneMenu id="createEditTaskSelect" value="#{unityScheduleMB.event.data.task}" converter="taskC" required="true">
                        <f:selectItems value="#{unityScheduleMB.taskList}" var="t" itemLabel="#{t.name}" itemValue="#{t}"/>
                    </h:selectOneMenu>

                    <h:outputLabel for="createEditDisciplineSelected" value="Disciplina:" />
                    <h:selectOneMenu id="createEditDisciplineSelected" value="#{unityScheduleMB.event.data.discipline}" required="true" converter="disciplineC">
                        <f:selectItems value="#{unityScheduleMB.disciplineList}" var="d" itemLabel="#{d.name}" itemValue="#{d}"/>
                    </h:selectOneMenu>

                    <h:outputLabel for="createEditDayOfWeekSelected" value="Dia:" />
                    <h:selectOneMenu id="createEditDayOfWeekSelected" value="#{unityScheduleMB.event.data.weekDay}" required="true">
                        <f:selectItems value="#{unityScheduleMB.daysOfWeek}" var="w" itemLabel="#{w.toString()}" itemValue="#{w}"/>
                    </h:selectOneMenu>

                    <h:outputLabel for="createEditFrom" value="Início:" />
                    <p:calendar id="createEditFrom" value="#{unityScheduleMB.event.startDate}" required="true" pattern="HH:mm"
                                requiredMessage="'Início' é campo obrigatório"/>

                    <h:outputLabel for="createEditTo" value="Fim:" />
                    <p:calendar id="createEditTo" value="#{unityScheduleMB.event.endDate}" required="true" pattern="HH:mm"
                                requiredMessage="'Fim' é campo obrigatório"/>
                    </h:panelGrid>
                    <div class="buttonGroup">
                        <p:commandButton value="Salvar"
                                         action="#{unityScheduleMB.saveEvent()}"  
                                         oncomplete="handleSaveRequest(xhr, status, args)"
                                         update=":scheduleForm:messages manageUEDetails"/>
                        <p:commandButton value="Excluir" rendered="#{unityScheduleMB.event.id != null}"
                                         process="@this" update=":scheduleForm:messages" 
                                         oncomplete="manageUEDialogW.hide(); uschedule.update();"
                                         action="#{unityScheduleMB.remove()}"
                                         immediate="true"/>
                        <p:commandButton value="Cancelar" onclick="manageUEDialogW.hide();"/>
                    </div>
            </h:form>

        </p:dialog>

        <script type="text/javascript">  
            function handleSaveRequest(xhr, status, args) {  
                if(!args.validationFailed) {
                    uschedule.update();
                    manageUEDialogW.hide();
                }   
            }
        </script>
    </ui:define>

</ui:composition>
